package com.aiju.cash.ui.good.manager;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aiju.base.dialog.BaseDialog;
import com.aiju.bean.Good;
import com.aiju.cash.R;
import com.aiju.common.PixValue;
import com.aiju.common.db.GoodService;
import com.aiju.utils.T;
import com.aiju.utils.Util;
import com.aiju.widgit.MaxListView;
import com.aiju.widgit.ShoppingView;
import com.aiju.widgit.dialog.EditWeighDialog;
import com.aiju.widgit.dialog.SimpleDialog;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * @author: 佐为
 * @date: 2016-12-21 16:18
 * @version: V1.0
 * @Description: 描述
 */
public class ShopCart extends BaseDialog {

    private Context context;
    private GoodAdapter mGoodAdapter;
    private GoodService service;
    private MaxListView mListView;

    private TextView topTextView;
    private TextView textViewTip;
    private TextView textViewNum;

    public ShopCart(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.LodingDialogStyle;
    }

    @Override
    protected View getView() {
        View root = mLayoutInflater.inflate(R.layout.dialog_shop, null);
        mListView = (MaxListView) root.findViewById(R.id.lv_shop_content);
        textViewTip = (TextView) root.findViewById(R.id.tv_biling_tip);
        textViewNum = (TextView) root.findViewById(R.id.tv_shop_money);
        topTextView= (TextView) root.findViewById(R.id.tv_shop_num);

        root.findViewById(R.id.tv_shop_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClearDialog();
            }
        });

        root.findViewById(R.id.tv_billing_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                GoodManager.getInstance(mContext).startOrderActivity();
            }
        });

        service = new GoodService(mContext);
        return root;
    }

    /**
     * 显示清空购物车对话框
     */
    private void showClearDialog() {
        new SimpleDialog(mContext)
                .setLayoutParams()
                .setTitileText("是否清空购物车")
                .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        GoodManager.getInstance(context).clearShopCart();
                        mGoodAdapter.notifyDataSetChanged();
                        changeView();
                        dismiss();
                    }
                })
                .setSystemAlert().show();
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public ShopCart setLayoutParams() {
        getDialogWindow().setWindowAnimations(R.style.share_animation);
        getDialogWindow().setGravity(Gravity.BOTTOM);
        getDialogWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        return this;
    }

    public ShopCart setSelectedGood(SparseArray<Good> selectedGoods) {
        mGoodAdapter = new GoodAdapter(selectedGoods);
        mListView.setAdapter(mGoodAdapter);
        mListView.setListViewHeight(PixValue.dp.valueOf(300));
        changeView();
        return this;
    }

    public void changeView() {
        topTextView.setText("共"+GoodManager.getInstance(context).getTotalSum()+"件");
        textViewTip.setText(GoodManager.getInstance(context).getTotalSum());
        textViewNum.setText(GoodManager.getInstance(context).getTotalPrice());
        if (GoodManager.getInstance(context).getTotalSum().equals("0")) {
            textViewTip.setVisibility(GONE);
        } else {
            textViewTip.setVisibility(VISIBLE);
        }
    }

    /**
     * 购物车列表适配器
     */
    private final class GoodAdapter extends BaseAdapter {
        private SparseArray<Good> shopEntities;

        public GoodAdapter(SparseArray<Good> shopEntities) {
            this.shopEntities = shopEntities;
        }

        @Override
        public int getCount() {
            return shopEntities.size();
        }

        @Override
        public Object getItem(int position) {
            return shopEntities.valueAt(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_dialog_shop, parent, false);
            TextView nameTextView = (TextView) inflate.findViewById(R.id.tv_dialog_shop_name);
            TextView priceTextView = (TextView) inflate.findViewById(R.id.tv_dialog_shop_money);

            final ShoppingView shoppingView = (ShoppingView) inflate.findViewById(R.id.shopping_view);
            final Good good = shopEntities.valueAt(position);

            shoppingView.getWeighTextView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    new EditWeighDialog(mContext)
                            .setTitileText(good.getName())
                            .setMsgText(good.getPrice() + "/" + good.getUnit())
                            .setContentText(Util.clearZero(good.getWeigh()))
                            .setLayoutParams()
                            .setOnEditClick(new EditWeighDialog.OnEditClick() {
                                @Override
                                public void onCancel() {
                                    shoppingView.setSum(0);
                                }

                                @Override
                                public void onSubmit(String msg) {
                                    if (TextUtils.isEmpty(msg)){
                                        T.ToastShort("请输入商品重量");
                                        shoppingView.setSum(0);
                                        return;
                                    }
                                    good.setWeigh(Double.valueOf(msg));
                                    GoodManager.getInstance(mContext).update(true);
                                }
                            })
                            .setCanceledOnTouchOutside(false)
                            .show();
                }
            });
            shoppingView.setOnAmountChangeListener(new ShoppingView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(int type, int sum) {
                    if (type == ShoppingView.ADD) {
                        GoodManager.getInstance(context).add(good, true);
                    } else {
                        GoodManager.getInstance(context).remove(good, true);
                    }
                    if (GoodManager.getInstance(context).getSelectedGoods().size() == 0) {
                        dismiss();
                    }
                }
            });
            if (good.isWeighGood()){
                shoppingView.setShowWeigh(good.getWeigh());
                priceTextView.setText(good.getPrice()+"/"+good.getUnit());
            }else {
                shoppingView.setSum(good.getSelectSum());
                priceTextView.setText(good.getPrice());
            }
            nameTextView.setText(good.getName());
            return inflate;
        }
    }

}
